<template>
    <div class="pending_approval" :style="{ 'height': productsHeight }">
        <div class="pending_approval_header">
            <div style="" class="product_list_serch-text">
                <van-search class="product_list_serch1" v-model="searchValue" shape="round" @search="onSearch"
                    placeholder="请输入商品名称" />
                <div class="product_list_serch2">
                    <div style="">
                        <van-button class="product_list-btn" round @click="onSearch">搜索</van-button>
                    </div>
                </div>
            </div>
        </div>
        <div class="approval_screen">
            <div class="approval_screen_category" @click="categoryShow = true">
                招商品类
                <van-image :src="dropDown"></van-image>
            </div>
            <div class="approval_screen_date" @click="dateShow = true">
                提交日期
                <van-image :src="dropDown"></van-image>
            </div>
            <van-action-sheet title="招商品类" v-model:show="categoryShow" :actions="categoryList"
                @select="onSelectCategory" />
            <van-action-sheet title="提交日期" v-model:show="dateShow" :actions="dateyList" @select="onSelectDate" />
        </div>
        <van-pull-refresh :style="{ 'height': refreshHeight }" v-model="refreshing" @refresh="onRefresh">
            <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
                <template v-slot:finished>
                    <div class="nothing_div" v-if="list.length == 0">
                        <van-image :src="nothingData"></van-image>
                        <div class="nothing_font">暂无数据</div>
                    </div>
                </template>
                <div class="product_list_van_list">
                    <div v-for="(item, index) in list" :key="index" class="product_list_van_list_div"
                        @click="handleGetDetails(item.id)">
                        <van-image :src="item?.productImages" class="van-image">
                            <template v-slot:error>加载失败</template>
                            <div class="liveStreamingMechanisms" v-if="item.hmMechanismsList[1]?.cooperationMode">多机制
                            </div>
                        </van-image>
                        <div class="div_right">
                            <div class="div_right_top">
                                <div class="style_name">
                                    <van-text-ellipsis :content="item.styleName" />
                                </div>
                                <div class="auditStatus" :style="auditStatusStyle(item.status)">
                                    {{ auditStatusNameById(item.status) }}
                                </div>
                            </div>
                            <div class="div_right_center">
                                <van-text-ellipsis rows="1" :content="item.sellingPoints" />
                            </div>
                            <div class="div_right_bottom">
                                <div class="price" v-if="item.hmMechanismsList[0]?.cooperationMode == '分佣'">￥ {{
                                    item.hmMechanismsList[0].price }}</div>
                                <div class="price" v-if="item.hmMechanismsList[0]?.cooperationMode == '供货'">￥ {{
                                    item.hmMechanismsList[0].costPrice }}</div>
                                <div class="costPrice" v-if="item.hmMechanismsList[0]?.cooperationMode == '分佣'">{{
                                    item.hmMechanismsList[0].costPrice }}</div>
                                <div class="commission" v-if='item.hmMechanismsList[0]?.commission'>
                                    佣 {{ (item.hmMechanismsList[0].commission || 0) + "%" }}
                                </div>
                            </div>
                            <div class="div_right_people">
                                <div class="launch">发起人： {{ item.recruiter }}</div>
                                <div class="approvals">审批人：{{ item.auditor }}</div>
                            </div>
                        </div>
                        <div class="div_right_times">
                            <div class="dates">发起时间：{{ item.createTime }}</div>
                            <div class="liveStreamingMechanism">{{ item.hmMechanismsList[0]?.liveStreamingMechanism }}
                            </div>
                        </div>
                    </div>
                </div>
            </van-list>
        </van-pull-refresh>
        <van-tabbar v-model="activeName" fixed @change="onChange" v-if="barFlag">
            <van-tabbar-item v-for="(tabbarItem, index) in tabbarList" :key="tabbarItem.id">
                <template #icon="">
                    <svg-icon :icon="tabbarItem.icon" v-if="index !== activeName" />
                    <svg-icon :icon="tabbarItem.icons" v-if="index == activeName" />
                </template>
                {{ tabbarItem.title }}
            </van-tabbar-item>
        </van-tabbar>
    </div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref, watch, onUnmounted } from 'vue';
import { auditStatusNameById } from "@/utils/statusNameById";
import { getEnvValue } from "@/utils/index";
import { useRoute, useRouter } from "vue-router";
import { getQualityList, getSystemList } from '@/api/approval/index'
const router = useRouter();
const route = useRoute();
const dropDown = new URL("@/assets/approval/drop_down .png", import.meta.url).href
const nothingData = new URL("@/assets/porduct/icon_zanwushuju.png", import.meta.url).href
const categoryShow = ref(false)
const categoryList = ref([])
const dateShow = ref(false)
const searchValue = ref("");
const dateyList = ref([
    {
        name: "全部",
        status: null,
    },
    {
        name: "近一周",
        status: 7,
    },
    {
        name: "近一月",
        status: 30,
    },
    {
        name: "近半年",
        status: 180,
    },
])

const refreshing = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = ref<any[]>([]);
const queryParams = ref<any>({
    pageNum: 1,
    pageSize: 10,
    // auditDept == 1 为品控部门列表
    auditDept: 1,
    // 通过驳回状态
    auditStatus: '',
    styleName: ''
});
interface TabItem {
    id: number,
    name: string,
    icon: string,
    icons: string,
    title: string,
    to: string
}
const activeName = ref();
const tabbarList: TabItem[] = [
    { id: 1, name: 'qualityControl', icon: 'icon_shouye', icons: '', title: '首页', to: '/qualityControl' },
    { id: 2, name: 'needdealt', icon: 'icon_daibanweixuanzhong', icons: 'icon_daibanxuanzhong', title: '待办', to: '/pendingQualityControl/待处理' },
    { id: 3, name: 'pass', icon: 'icon_yitongguoweixuanzhong', icons: 'icon_yitongguoxuanzhong', title: '已通过', to: '/pendingQualityControl/已通过' },
    { id: 4, name: 'rejected', icon: 'icon_yibohuiweixuanzhong', icons: 'icon_yibohuixuanzhong', title: '已驳回', to: '/pendingQualityControl/已驳回' },
]
const defaultPhoneHeight = ref(0) //屏幕默认高度
const nowPhoneHeight = ref(0) //屏幕现在的高度
const productsHeight = ref()
const refreshHeight = ref()
const barFlag = ref(true)

// 限制连续刷新数据重复问题
const refreFlag = ref(true)

const onSearch = () => {
    queryParams.value.styleName = searchValue.value
    onRefresh()
}

/** 重置筛选状态 */
const resetScreen = () => {
    // 招商品类
    queryParams.value.recruiterDeptId = ''
    // 日期
    queryParams.value.startTime = ''
    categoryList.value.filter((item: any) => {
        item.color = ''
    })
    dateyList.value.filter((item: any) => {
        item.color = ''
    })
    onRefresh()
}

const onChange = async (index: number) => {
    // 待办
    if (index == 1) {
        queryParams.value.auditStatus = '0'
        router.push('/pendingQualityControl/待处理')
        resetScreen()
    } else if (index == 2) {
        // 已通过
        queryParams.value.auditStatus = '2'
        router.push('/pendingQualityControl/已通过')
        resetScreen()
    } else if (index == 3) {
        queryParams.value.auditStatus = '1'
        router.push('/pendingQualityControl/已驳回')
        resetScreen()
    } else {
        router.push('/qualityControl');
    }
};

/** 获取招商品类列表 */
const getsList = async () => {
    const res = await getSystemList({ parentId: '1796913597400784898' })
    if (res.data.length) {
        res.data.unshift({
            deptName: '全部',
            deptId: null
        })
        res.data.forEach(item => {
            item.name = item.deptName
            item.status = item.deptId
        })
        categoryList.value = res.data
    }
}
getsList()

/** 标题 */
const names: any = computed(() => {
    return route.params.name;
});

/** 状态颜色 */
const auditStatusStyle = (auditStatus: string) => {
    // 待处理
    if (auditStatus == '0') {
        return {
            'background': '#EBF4FF',
            'color': '#0094FF'
        }
        // 已通过
    } else if (auditStatus == '2') {
        return {
            'background': '#E7FDEE',
            'color': '#19D11B'
        }
        //已驳回
    } else if (auditStatus == '1') {
        return {
            'background': '#FDE7E7',
            'color': '#FC2540'
        }
    }
}
/** 核价类型选择事件 */
const onSelectCategory = (selection) => {
    categoryShow.value = false;
    queryParams.value.recruiterDeptId = selection.deptId
    categoryList.value.filter((item: any) => {
        if (item.deptId == selection.deptId) {
            item.color = '#0094FF'
        } else {
            item.color = ''
        }
    })
    onRefresh()
}
/** 提交日期选择事件 */
const onSelectDate = (selection) => {
    dateShow.value = false;
    queryParams.value.startTime = selection.status
    dateyList.value.filter((item: any) => {
        if (item.status == selection.status) {
            item.color = '#0094FF'
        } else {
            item.color = ''
        }
    })
    onRefresh()
}
/** 查询商品列表 */
const getList = async () => {
    const res = await getQualityList(queryParams.value);
    res.rows.forEach(async (item) => {
        if (item.productImage) {
            item.productImages = getEnvValue("VITE_APP_PIC_URL") + item.productImage.split(",")[0]
        } else {
            item.productImages = "";
        }
    });
    setTimeout(() => {
        refreFlag.value = true
        // 空数据 停止加载
        if (res.rows.length == 0) {
            list.value = [];
            finished.value = true;
            return;
        }
        // 有数据 赋值列表 加载状态结束
        res.rows.forEach((item) => {
            finished.value = false;
            list.value.push(item);
        });
        loading.value = false;
        refreshing.value = false;
        // 如果list长度大于等于总数据条数，数据全部加载完成
        if (list.value.length >= res.total) {
            finished.value = true; //结束加载
        }
        // 解决快速切换tab导致的列表商品混乱问题
        if (queryParams.value.pageNum == 2 && queryParams.value.auditStatus) {
            list.value = list.value.filter((item) => {
                return item.status == queryParams.value.auditStatus
            })
        }
    }, 400);
};
/** 加载更多 */
const onLoad = async () => {
    getList();
    queryParams.value.pageNum++;
};
/** 下拉 */
const onRefresh = () => {
    if (refreFlag.value) {
        refreFlag.value = false
        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        queryParams.value.pageNum = 1; // 分页数赋值为1
        list.value = []; // 清空数组
        onLoad(); // 重新加载数据
    } else {
        finished.value = false;
        refreshing.value = false;
    }
};
/** 获取详情 */
const handleGetDetails = (id: string) => {
    router.push({ path: `/pendingQualityControlDetail/${id}`, query: { name: names.value } })
};
onMounted(() => {
    if (names.value == '已驳回') {
        queryParams.value.auditStatus = '1'
        activeName.value = 3
    } else if (names.value == '已通过') {
        queryParams.value.auditStatus = '2'
        activeName.value = 2
    } else {
        queryParams.value.auditStatus = '0'
        activeName.value = 1
    }
    defaultPhoneHeight.value = window.innerHeight
    window.addEventListener('resize', handleResize);
})
// 处理窗口大小变化的函数
const handleResize = () => {
    nowPhoneHeight.value = window.innerHeight;
};
// 在组件卸载前移除事件监听器
onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
});
watch(nowPhoneHeight, () => {
    if (defaultPhoneHeight.value != nowPhoneHeight.value) {
        // 当软键盘弹出,改变bottomIsSHow变量然后去控制bottom的显示与隐藏
        barFlag.value = false
        productsHeight.value = '100vh'
        refreshHeight.value = 'calc(100vh - 41.0vw)'
    } else {
        // 相等代表软键盘收起后,与上同理
        barFlag.value = true
        productsHeight.value = 'calc(100vh - 13.3vw)'
        refreshHeight.value = 'calc(100vh - 54.4vw)'
    }
}, { immediate: true })
</script>
<style lang="less" scoped>
.pending_approval {
    background-color: #F6FAFF;
    width: 100%;
    height: calc(100vh - 44px);
    border-radius: 5px;
    box-sizing: border-box;

    :deep(.van-image__img) {
        border-radius: 5px !important;
    }

    .liveStreamingMechanisms {
        position: absolute;
        top: 0;
        left: 0;
        width: 50px;
        height: 20px;
        background: #0094FF;
        border-radius: 2px 0px 2px 0px;
        font-weight: 400;
        font-size: 12px;
        color: #FFFFFF;
        text-align: center;
        line-height: 20px;
    }

    .nothing_div {
        width: 100%;
        margin: auto;
        text-align: center;
        margin-top: 20vh;
        font-weight: 400;
        font-size: 12px;
        color: #B8BECA;
        line-height: 12px;
        flex-wrap: wrap;

        .van-image {
            width: 160px;
            height: 160px;
        }

    }

    .pending_approval_header {
        width: 100%;
        height: 110px;
        padding-top: 44px;
        box-sizing: border-box;
        background: linear-gradient(#0064FF 0%, #008FFF 100%);

        .product_list_serch1 {
            width: 100%;
            height: 50px;
            margin-top: 10px;
            background: none;
        }

        .product_list_serch-text {
            display: flex;
            align-items: center;
            width: 100%;
            height: 50px;
            position: relative;
        }

        .product_list_serch2 {
            position: absolute;
            right: 20px;
            top: calc(50% + 10px);
            transform: translateY(-50% - 18px);

            :deep(.van-button__text) {
                font-size: 14px;
            }

            .product_list-btn {
                width: 54px;
                height: 26px;
                font-size: 14px;
                color: #ffffff;
                border: none;
                background: #0094ff;
                white-space: nowrap;
            }
        }
    }

    .approval_screen {
        height: 44px;
        width: 100%;
        background-color: #fff;
        display: flex;
        justify-content: space-around;
        align-items: center;
        font-size: 14px;

        :deep(.van-image) {
            width: 10px;
            height: 6px;
            margin-left: 4px;
        }

        .approval_screen_category {
            display: flex;
            align-items: center;
        }

        .approval_screen_date {
            display: flex;
            align-items: center;
        }
    }

    .product_list_van_list {
        padding: 0 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        margin-top: 10px;
        background: #f6faff;

        .van-image {
            width: 80px;
            height: 80px;
        }

        &_div {
            width: 100%;
            height: 130px;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 3px;
            margin-bottom: 8px;
            padding: 5px;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            padding: 10px 15px 10px 15px;
            display: flex;
            align-items: center;
        }

        .div_right {
            width: calc(100% - 90px);
            display: flex;
            flex-wrap: wrap;

            .div_right_top {
                margin-top: -5px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: space-between;
                // padding-left: 10px;

                .style_name {
                    font-size: 14px;
                    font-weight: bold;
                    width: 150px;
                    display: -webkit-box;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    -webkit-line-clamp: 1;
                    -webkit-box-orient: vertical;
                }

                .auditStatus {
                    font-size: 14px;
                    border-radius: 2px;
                    padding: 3px 13px 3px 13px;
                    text-align: center;
                    border-radius: 3px;
                }

                .stats {
                    background: #fde7e7;
                    border-radius: 2px;
                    padding: 3px 13px 3px 13px;
                    font-size: 14px;
                    color: #fc2540;
                    text-align: center;
                    border-radius: 3px;
                }
            }

            .div_right_center {
                // padding-top: 5px;
                margin-top: 5px;
                // padding-left: 10px;
                width: 100%;
                // height: 28px;
                font-size: 12px;
                color: #b8beca;
                display: -webkit-box;
                overflow: hidden;
                text-overflow: ellipsis;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
            }

            .div_right_bottom {
                margin-top: 2px;
                margin-bottom: 3px;
                // padding-left: 10px;
                display: flex;
                width: 100%;
                align-items: center;
                justify-content: space-between;

                .price {
                    font-size: 12px;
                    color: #fc2540;
                    font-weight: 500;
                    white-space: nowrap;
                }

                .costPrice {
                    font-size: 11px;
                    color: #a3a3a3;
                    font-weight: 500;
                    white-space: nowrap;
                    margin-left: 10px;
                }

                .commission {
                    font-weight: 500;
                    font-size: 12px;
                    color: #fc2540;
                    // border: solid 1px black;
                    white-space: nowrap;
                    width: 20%;
                    margin-left: 20px;
                }

                .liveStreamingMechanism {
                    font-size: 11px;
                    color: #a3a3a3;
                    margin-left: 15px;
                    //   width: 20%;
                    // text-align: right;
                }

                .anchor {
                    margin-left: auto;
                    padding-left: 15px;
                    font-weight: 400;
                    font-size: 12px;
                    color: #b8beca;
                    //   width: 20%;
                    //   border: 1px solid #ffb0b0;
                    //   padding: 1px 2px;
                    text-align: center;
                    white-space: nowrap;
                    /* 确保文本在一行内显示 */
                    overflow: hidden;
                    /* 隐藏溢出的内容 */
                    text-overflow: ellipsis;
                }
            }

            .div_right_people {
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-size: 12px;
                // padding-left: 10px;
                width: 100%;
                flex-wrap: wrap;

                .approvals {
                    // margin-left: 5px;
                    // min-width: 20px;
                }
            }
        }

        .div_right_times {
            width: 100%;
            height: 20px;
            font-size: 12px;
            color: #B8BECA;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
    }

    :deep(.van-pull-refresh) {
        height: calc(100vh - 204px);
        overflow: scroll;
    }

    :deep(.van-action-sheet__header) {
        font-weight: 500 !important;
        font-size: 16px !important;
        color: #1a1b1c !important;
    }

    :deep(.van-action-sheet__item) {
        font-size: 14px;
        color: #b8beca;
    }

    :deep(.van-tabbar) {
        height: 50px !important;
    }
}
</style>